<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/6
 * Time: 10:24
 */
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;

class Parents extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';
    const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    // π
    const pi = 3.1415926535897932384626;
    // 长半轴
    const a = 6378245.0;
    // 扁率
    const ee = 0.00669342162296594323;
    public function _initialize()
    {
        parent::_initialize();
    }

    public function clock_in()
    {

    }


    //消息
    public function message()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $time = date('Y-m-d');

        $params = input('get.');
        $id = $this->auth->id;
        $where = [];
        //$where['a.DTIME'] = $time;
        /*if(isset($params['class_room_id']) && $params['class_room_id']!='')
        {
            $where['b.class_room_id'] = $params['class_room_id'];
        }
        if(isset($params['circuit_id']) &&$params['circuit_id']!='')
        {
            $where['c.id'] = $params['circuit_id'];
        }*/
        $cars = Db::name('car')->field('IMEI,license_plate')->where("IMEI!=''")->select();
        $cars_arr = [];
        if($cars)
        {
            foreach ($cars as $key=>$value)
            {
                $cars_arr[$value['IMEI']] = $value['license_plate'];
            }
        }

        $where['b.parent_id'] = $id;
        $list = Db::name('on_off_car')->alias('a')
            ->join('fa_student_information b','a.RC_NUM=b.RC_NUM','inner')
            //->join('fa_circuit c','a.circuit_id=c.id')
            ->where($where)
            //->field('b.name,b.class_room_id,b.student_number,a.RC_TYPE,a.TIME,c.title')->select();
            ->field('b.name,a.RC_TYPE,a.TIME,a.circuit,a.IMEI,a.class_room')
            ->select();

        if($list)
        {
            foreach ($list as $k=>&$v)
            {

                $v['on_time'] = date('Y-m-d H:i:s',$v['TIME']);

                $v['license_plate'] = isset($cars_arr[$v['IMEI']])?$cars_arr[$v['IMEI']]:'';
                $v['status'] = '丢卡';
            }
            $this->success('',$list);
        }
        else
        {
            $this->error('暂无数据',[]);
        }

    }

    //我的孩子
    public function getChild()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $id = $this->auth->id;
        $list = Db::name('student_information')->where('parent_id',$id)->field('id,name')->select();
        $this->success('',$list);
    }

    //请假
    public function leave()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }
        $id = $this->auth->id;
        $info = $this->auth->getUserinfo();
        $params = input('post.');
        if($params['student_id']=='')
        {
            $this->error('请假学生不能为空');
        }
        if($params['status']=='')
        {
            $this->error('请假理由不能为空');
        }
        if($params['start_time']=='')
        {
            $this->error('开始时间不能为空');
        }
        if($params['end_time']=='')
        {
            $this->error('结束时间不能为空');
        }
        if($params['day']=='')
        {
            $this->error('时长不能为空');
        }
        if($params['content']=='')
        {
            $this->error('请假理由不能为空');
        }
        $time = time();
        $params['start_time'] = strtotime($params['start_time']);
        $params['end_time'] = strtotime($params['end_time']);
        $params['apply_id'] = $id;
        $params['role'] = $info['role'];
        $params['createtime'] = $time;
        $params['updatetime'] = $time;
        $res = Db::name('leave')->insert($params);
        if($res)
        {
            $this->success('提交成功');
        }
        else
        {
            $this->error('提交失败');
        }
    }

    //请假列表
    public function leave_list()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        //$userInfo = $this->auth->getUserinfo();
        $id = $this->auth->id;
        $params = input('get.');

        $list = Db::name('leave')->alias('a')
            ->join('fa_student_information b','a.student_id=b.id')
            ->field('a.*,b.name')
            ->where('b.parent_id',$id)
            ->paginate($params['listRows'],false,['page'=>$params['page']])->toArray();

        if($list['data'])
        {
            foreach ($list['data'] as $k=>&$v)
            {

                $v['start_time'] = date('Y-m-d H:i',$v['start_time']);
                $v['end_time'] = date('Y-m-d H:i',$v['end_time']);
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
                $v['approval_time'] = date('Y-m-d H:i:s',$v['approval_time']);
            }
        }
        $this->success('',$list);

    }

    //获取老师，司机信息
    public function get_base()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $id = $this->auth->id;

        /*$teacher = Db::name('teacher')->order('id','desc')->field('name,telephone as mobile')->limit(1)->find();

        $driver =  Db::name('driver')->field('name,mobile')->limit(1)->find();*/


        $teacher = Db::name('student_information')->alias('a')
            ->join('fa_admin b','a.class_room_id=b.class_room_id')
            ->where('a.parent_id',$id)
            ->field('b.nickname,b.mobile,a.circuit_id')
            ->select();
       /* $where = [];
        if($teacher)
        {
            foreach ($teacher as $k=>$v)
            {
                if(!empty($v['circuit_id']))
                {
                    $where['b.circuit_id'] = $v['circuit_id'];
                    break;
                }
            }
        }*/

        /*$driver = Db::name('driver')->alias('a')
            ->join('fa_car b','a.id=b.driver_id')
            ->order('b.id','desc')
            ->where($where)->field('a.name,a.mobile')
            ->limit(1)
            ->find();*/

        $this->success('',$teacher);
    }

    //获取多线路点位
    public function all_points()
    {
        $userInfo = $this->auth->getUserinfo();

        $type = $userInfo['role'];
        $list = [];
        if($type==1)
        {

            $list = Db::name('student_information')->alias('a')
                ->join('fa_circuit b','a.circuit_id=b.id')
                ->field('b.title,b.id,b.car_id,b.id')
                ->where('a.parent_id',$userInfo['id'])
                ->select();

        }
        else if($type==2)
        {
            $list = Db::name('student_information')->alias('a')
                ->join('fa_circuit b','a.circuit_id=b.id')
                ->field('b.title,b.id,b.car_id,b.id')
                ->where('a.class_room_id',$userInfo['class_room_id'])
                ->select();
        }
        else if($type==3)
        {
            $list = Db::name('circuit')->select();
        }

        if(!$list)
        {
            $this->error('暂无数据');
        }
        $circuits = [];
        $circuit_title = [];
        if($list)
        {
            foreach ($list as $k=>$v)
            {
                $circuits[$v['id']] = $v['id'];
                $circuit_title[$v['car_id']] = $v['title'];
            }
        }

        $circuits = array_unique($circuits);
        $car = Db::name('circuit')->alias('b')
            ->join('fa_car a','a.id=b.car_id')
            ->field('a.id,a.license_plate,a.pinyin,a.IMEI,b.title')->where(['b.id'=>['in',$circuits]])->select();//

        if(!$car)
        {
            $this->error('暂无数据');
        }

        $data = [];
        $where = [];

        //$where['DTIME'] = date('Y-m-d');
        foreach ($car as $key=>$value)
        {
            $table_name = 'car_'.$value['pinyin'];
            $circuit_name = $value['title'];

            $car_zhe = Db::name($table_name)->where('IMEI',$value['IMEI'])->where($where)->order('id','asc')->select();
            if($car_zhe)
            {
                $data[$circuit_name] = $car_zhe;
            }
        }
        if($data)
        {
            $this->success('',$data);
        }
        else
        {
            $this->error('暂无数据');
        }

    }

    public function all_new_points()
    {
        $userInfo = $this->auth->getUserinfo();
        $type = $userInfo['role'];
        $list = [];
        if($type==1)
        {

            $list = Db::name('student_information')->alias('a')
                ->join('fa_circuit b','a.circuit_id=b.id')
                ->field('b.title,b.id,b.car_id,b.id')
                ->where('a.parent_id',$userInfo['id'])
                ->select();

        }
        else if($type==2)
        {
            $list = Db::name('student_information')->alias('a')
                ->join('fa_circuit b','a.circuit_id=b.id')
                ->field('b.title,b.id,b.car_id,b.id')
                ->where('a.class_room_id',$userInfo['class_room_id'])
                ->select();
        }
        else if($type==3)
        {
            $list = Db::name('circuit')->select();
        }

        if(!$list)
        {
            $this->error('暂无数据');
        }
        $circuits = [];
        $circuit_title = [];
        if($list)
        {
            foreach ($list as $k=>$v)
            {
                $circuits[$v['id']] = $v['id'];
                $circuit_title[$v['car_id']] = $v['title'];
            }
        }

        $circuits = array_unique($circuits);
        $car = Db::name('circuit')->alias('b')
            ->join('fa_car a','a.id=b.car_id')
            ->field('a.id,a.license_plate,a.pinyin,a.IMEI,b.title')->where(['b.id'=>['in',$circuits]])->select();//

        if(!$car)
        {
            $this->error('暂无数据');
        }

        $data = [];
        $where = [];

        //$where['DTIME'] = date('Y-m-d');
        foreach ($car as $key=>$value)
        {
            $table_name = 'car_'.$value['pinyin'];
            $circuit_name = $value['title'];

            $car_zhe = Db::name($table_name)->where('IMEI',$value['IMEI'])->where($where)->order('id','desc')->limit(1)->find();
            if($car_zhe)
            {
                $data[$circuit_name] = $car_zhe;
            }
        }
        if($data)
        {
            $this->success('',$data);
        }
        else
        {
            $this->error('暂无数据');
        }
    }



    public function get_points()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $params = input('get.');

        if(!isset($params['IMEI']) || $params['IMEI']=='')
        {
            $this->error('缺少参数');
        }
        if(!isset($params['pinyin']) || $params['pinyin']=='')
        {
            $this->error('缺少参数');
        }

        $IMEI =$params['IMEI'];
        $tableName = 'car_'.$params['pinyin'];
        $list = Db::name($tableName)->where(['IMEI'=>$IMEI])->order('id','asc')->select();

        if($list)
        {
            foreach ($list as $k=>$v)
            {
                if($v['GPS_LNG']=='' || $v['GPS_LAT']=='' )
                {
                    unset($list[$k]);
                    continue;
                }

                $jindu = self::wgs84ToGcj02($v['GPS_LNG'],$v['GPS_LAT']);
                $list[$k]['GPS_LNG'] = $jindu[0];
                $list[$k]['GPS_LAT'] = $jindu[1];
            }
            $this->success('',$list);
        }

        $this->error('暂无数据');


    }

    public function point()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $params = input('get.');
        if(!isset($params['IMEI']) || $params['IMEI']=='')
        {
            $this->error('缺少参数');
        }
        if(!isset($params['pinyin']) || $params['pinyin']=='')
        {
            $this->error('缺少参数');
        }
        /*$id = $this->auth->id;
        $teacher = Db::name('student_information')->where(['parent_id'=>$id,'circuit_id'=>['neq',0]])->find();
        $car = Db::name('car')->where('circuit_id',$teacher['circuit_id'])->find();


        $DTIME = date('Y-m-d');
        $IMEI = $car['IMEI'];*/

        //$tableName = 'car_'.$car['pinyin'];

        //$IMEI ='861714055270312';
        $IMEI =$params['IMEI'];
        $tableName = 'car_'.$params['pinyin'];
        $info = Db::name('car_zheb578')->where(['IMEI'=>$IMEI])->order('id','desc')->find();
        //$info = Db::name($tableName)->where(['IMEI'=>$IMEI])->order('id','desc')->find();
        if($info)
        {
            $jindu = self::wgs84ToGcj02($info['GPS_LNG'],$info['GPS_LAT']);

            $info['GPS_LNG'] = $jindu[0];
            $info['GPS_LAT'] = $jindu[1];
        }

        $this->success('',$info);
    }

    public static function wgs84ToGcj02 (float $lng, float $lat): array {
        $d_lat = self::transformlat($lng - 105.0, $lat - 35.0);
        $d_lng = self::transformlng($lng - 105.0, $lat - 35.0);
        $rad_lat = $lat / 180.0 * self::pi;
        $magic = sin($rad_lat);
        $magic = 1 - self::ee * $magic * $magic;
        $sqrt_magic = sqrt($magic);
        $d_lat = ($d_lat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrt_magic) * self::pi);
        $d_lng = ($d_lng * 180.0) / (self::a / $sqrt_magic * cos($rad_lat) * self::pi);
        $mg_lat = $lat + $d_lat;
        $mg_lng = $lng + $d_lng;
        return [$mg_lng, $mg_lat];
    }

    /**
     * 纬度转换
     * @param float $lng
     * @param float $lat
     * @return float|int
     */
    public static function transFormLat (float $lng, float $lat): float {
        $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
        $ret += (20.0 * sin(6.0 * $lng * self::pi) + 20.0 * sin(2.0 * $lng * self::pi)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lat * self::pi) + 40.0 * sin($lat / 3.0 * self::pi)) * 2.0 / 3.0;
        $ret += (160.0 * sin($lat / 12.0 * self::pi) + 320 * sin($lat * self::pi / 30.0)) * 2.0 / 3.0;
        return $ret;
    }

    /**
     * 经度转换
     * @param float $lng
     * @param float $lat
     * @return float
     */
    public static function transFormLng (float $lng, float $lat): float {
        $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
        $ret += (20.0 * sin(6.0 * $lng * self::pi) + 20.0 * sin(2.0 * $lng * self::pi)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lng * self::pi) + 40.0 * sin($lng / 3.0 * self::pi)) * 2.0 / 3.0;
        $ret += (150.0 * sin($lng / 12.0 * self::pi) + 300.0 * sin($lng / 30.0 * self::pi)) * 2.0 / 3.0;
        return $ret;
    }



}